#include "msgProc_32.h"

int acceptfd;
void *getQueueHandler()
{
    while (1)
    {
        struct msg ms;
        msg_queue_recv("msg", &ms, sizeof(ms), 32, 0); 
        printf("-------------------%s---------------%d\n",ms.buf,__LINE__);
        send(acceptfd,ms.buf,strlen(ms.buf),0);
    }
}

int main(int argc, char const *argv[])
{
    // 1.创建套接字
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        perror("socket err:");
        return -1;
    }
    // 2.填充结构体（ipv4）
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;                          // 协议族ipv4
    addr.sin_port = htons(8888);                        // 端口号（网络字节序）
    addr.sin_addr.s_addr = inet_addr("0.0.0.0"); // ip地址（网络字节序）

    // 3.绑定
    int ret = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
    if (ret < 0)
    {
        perror("bind err:");
        return -1;
    }

    // 4.监听
    if (listen(sockfd, 5) < 0)
    {
        perror("listen err");
        return -1;
    }

    // 5.等待连接
    acceptfd = accept(sockfd, NULL, NULL);
    if (acceptfd < 0)
    {
        perror("accpet err:");
        return -1;
    }
    printf("连接成功111\n");
    pthread_t tid;
    pthread_create(&tid, NULL, getQueueHandler, NULL);
    pthread_detach(tid);
    while (1)
    {
        // 6.接受到来自stm32的数据并放入共享内存
        char buf[N] = {};
        ret = recv(acceptfd, buf, N, 0);
        if (ret == -1)
        {
            perror("recv err:");
            return -1;
        }
        printf("-------------------%s---------------%d\n",buf,__LINE__);
        cJSON *root = cJSON_Parse(buf);
        if (root == NULL)
        {
            printf("json格式错误--%d\n", __LINE__);
            continue;
        }
        struct shm_param p1;
        shm_init(&p1, "shm", 128);
        int *p = shm_getaddr(&p1);
        cJSON *data = cJSON_GetObjectItem(root, "data");
        struct std_node* st = (struct std_node *)(p+1);
        for (int i = 0; i < cJSON_GetArraySize(data); i++)
        {
            int key = cJSON_GetObjectItem(cJSON_GetArrayItem(data,i),"key")->valueint;
            for (int j = 0; j < *p; j++)
            {
                if (st->key == key)
                {
                    switch (st->type)
                    {
                    case 1:
                        st->new_val.b_val = cJSON_GetObjectItem(cJSON_GetArrayItem(data,i),"val")->valueint;
                        break;
                    case 2:
                        st->new_val.i_val = cJSON_GetObjectItem(cJSON_GetArrayItem(data,i),"val")->valueint;
                        break;
                    case 3:
                        st->new_val.f_val = cJSON_GetObjectItem(cJSON_GetArrayItem(data,i),"val")->valuedouble;
                        break;
                    default:
                        break;
                    }
                    break;
                }
                st++;
            }
        }
    }

    // 7.关闭
    close(acceptfd);
    close(sockfd);
    return 0;
}
